package com.yjx.controller;

import com.github.pagehelper.PageInfo;
import com.yjx.conent.ResultCode;
import com.yjx.dto.ResourceDto;
import com.yjx.dto.RoleDto;
import com.yjx.entity.Role;
import com.yjx.entity.User;
import com.yjx.service.*;
import com.yjx.util.Result;
import com.yjx.util.ResultUtil;
import com.yjx.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2023-04-15 16:30:23
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private OperateTokenService operateTokenService;
    @Resource
    private UserTokenService userTokenService;
    @Resource
    private ResourceService resourceService;

    @PostMapping("/login")
    public UserVo login(@RequestBody User user) {

        return userService.login(user);
    }
    @PostMapping("/getOperateToken")
    public Result getOperateToken(){
        String uuid=operateTokenService.getOperateToken();
        OperateTokenVo operateTokenVo=new OperateTokenVo();
        operateTokenVo.setOperateToken(uuid);
        return ResultUtil.successDataResult(operateTokenVo);
    }

    @PostMapping("/checkRoleCode")
    public Result checkRoleCode(@RequestParam("roleCode") String roleCode){
        Boolean result = roleService.checkRoleCode(roleCode);
        if (result){
            return ResultUtil.successResult();
        }
        return ResultUtil.errorResult(ResultCode.ERROR_CHECK_CODE,ResultCode.ERROR_CHECK_CODE_MSG);
    }
    @PostMapping("/createRole")
    public Result saveRole(@RequestBody Role role,
                            @RequestParam("token") String token,
                           @RequestParam("operateToken") String operateToken){
        Integer integer = roleService.saveRole(role, token,operateToken);
        if (integer!=null){
            return ResultUtil.successResult();
        }
        return ResultUtil.errorResult(ResultCode.ERROR_CODE,ResultCode.ERROR_MSG);
    }
    @PostMapping("/deleteRoleById")
    public Result deleteRoleById(@RequestParam("id")Integer id){
        Integer result = roleService.updateIsDeleteById(id);
        if (result>0){
            return ResultUtil.successResult();
        }
        return ResultUtil.errorResult(ResultCode.ERROR_CODE,ResultCode.ERROR_MSG);
    }
    @PostMapping("/checkUserCode")
    public Result checkUserCode(@RequestParam("userCode") String userCode){
        User user = userService.queryBuUserCode(userCode);
        if (user == null){
            return ResultUtil.successResult();
        }
        return ResultUtil.errorResult(ResultCode.ERROR_CHECK_CODE,ResultCode.ERROR_CHECK_CODE_MSG);
    }

    //创建员工
    @PostMapping("/createUser")
    public Result createUser(@RequestBody User user,
                             @RequestParam("token")String token){
        Integer count = userService.createUsr(user, token);
        if (count>0){
            return ResultUtil.successResult();
        }
        return ResultUtil.errorResult(ResultCode.ERROR_CODE,ResultCode.ERROR_MSG);
    }
    @PostMapping("/queryAllResource")
    public Result<List<ParentResourceVo>> queryAllResource(){
        List<ParentResourceVo> parentResourceVoList = resourceService.queryAllResource();
        return ResultUtil.successDataResult(parentResourceVoList);
    }

    @PostMapping("/saveResource")
    public Result saveResource(@RequestBody ResourceDto resourceDto, @RequestParam("token") String token){
        resourceService.saveResource(resourceDto,token);
        return ResultUtil.successResult();
    }
    @PostMapping("queryAllRole")
    public Result<List<RoleVo>> queryAllRole(){
        List<RoleVo> roleVos = roleService.queryAllRole();
        return ResultUtil.successDataResult(roleVos);
    }
    @Resource
    private UserRoleService userRoleService;
    @PostMapping("createUserRole")
    public Integer createUserRole(@RequestBody RoleDto roleDto){
        return userRoleService.createUserRole(roleDto);
    }

    @PostMapping("/queryRoleByRoleName")
    public Result<PageVo<RoleVo>> queryRoleByRoleName(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(value = "roleName",required = false,defaultValue = "") String roleName){
        PageInfo<RoleVo> pageInfo = roleService.queryRoleByRoleName(roleName, pageNum, pageSize);
        PageVo<RoleVo> pageVo=new PageVo<>();
        pageVo.setPageNum(pageNum);
        pageVo.setPageSize(pageSize);
        pageVo.setPages(pageInfo.getPages());
        pageVo.setList(pageInfo.getList());
        return ResultUtil.successDataResult(pageVo);
    }

    @PostMapping("/queryUserByUserName")
    public Result<PageVo<UserVo>> queryUserByUserName(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(value = "userName",required = false,defaultValue = "") String userName) {
        PageInfo<UserVo> pageInfo = userService.queryUserByUserName(userName, pageNum, pageSize);
        PageVo<UserVo> pageVo = new PageVo<>();
        pageVo.setPageNum(pageNum);
        pageVo.setPageSize(pageSize);
        pageVo.setList(pageInfo.getList());
        pageVo.setPages(pageInfo.getPages());
        return ResultUtil.successDataResult(pageVo);
    }


}
